// pages/login/login.js
const app = getApp();
const md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_num: '',
    login_pass: ''
  },
  forget_pas() {
    wx.navigateTo({
      url: 'findPass/findPass'
    })
  },
  //清除
  click_clear(){
    this.setData({
      login_num:'',
      flag_user:false,
    })
  },
  //用户账号
  user_input: function(e) {
    let login_num = e.detail.value;
    this.setData({
      login_num: login_num,
    })
    if (login_num && this.data.login_pass) {
      this.setData({
        flag_user: true,
      })
    } else {
      this.setData({
        flag_user: false,
      })
    }
  },
  //用户密码
  pass_input: function(e) {
    let login_pass = e.detail.value;
    this.setData({
      login_pass: e.detail.value,
    })
    if (login_pass && this.data.login_num) {
      this.setData({
        flag_user: true,
      })
    } else {
      this.setData({
        flag_user: false,
      })
    }
  },
  //点击登录
  login_click: function() {
    //     console.log(this.data.login_num)
    //     console.log(md5.hex_md5(this.data.login_pass))
    var that = this;
    if (this.data.flag_user) {
      if (this.data.login_num == '' || this.data.login__pass == '') {
        wx.showToast({
          title: '手机号或密码不能为空',
          icon: 'none'
        })
      } else {
        wx.request({
          method: 'POST',
          url: app.globalData.url + 'api/sign/telephone/count',
          data: {
            telephone: this.data.login_num
          },
          success: function(e) {
            // console.log(e)
            if (e.data.code == 0) {
              if (e.data.data.count == 1) {
                var _password = md5.hex_md5(that.data.login_pass)
                // console.log(_password)
                wx.request({
                  method: 'POST',
                  url: app.globalData.url + 'api/sign/login',
                  data: {
                    telephone: that.data.login_num,
                    password: _password,
                    from: "WxMiNi",
                  },
                  success: function(e) {
                    if (e.data.code == -1) {
                      wx.showToast({
                        title: e.data.message,
                        icon: 'none'
                      })
                    } else {
                      // console.log(e.data.data.register)

                      var register_info = e.data.data.register;

                      //token
                      app.globalData.token = register_info.token;
                      wx.setStorageSync('token', register_info.token)

                      //昵称
                      app.register_info.nickname = register_info.nickname;
                      wx.setStorageSync('reg_nickname', register_info.nickname)
                      //手机号
                      app.register_info.telephone = register_info.telephone;
                      wx.setStorageSync('reg_telephone', register_info.telephone)
                      //性别
                      app.register_info.sex = register_info.sex;
                      wx.setStorageSync('reg_sex', register_info.sex)
                      //头像地址
                      app.register_info.avatar_url = register_info.avatar_url;
                      wx.setStorageSync('reg_avatar_url', register_info.avatar_url)
                      //用户角色
                      app.register_info.register_role = register_info.register_role;
                      wx.setStorageSync('reg_register_role', register_info.register_role)
                      //签名
                      app.register_info.signature = register_info.signature;
                      wx.setStorageSync('reg_signature', register_info.signature)
                      //邀请码
                      app.register_info.invite_code = register_info.invite_code;
                      wx.setStorageSync('reg_invite_code', register_info.invite_code)
                      //微信Id
                      app.register_info.wxinfo_id = register_info.wxinfo_id;
                      wx.setStorageSync('reg_wxinfo_id', register_info.wxinfo_id)



                      var _token = wx.getStorageSync('token');
                      if (app.globalData.is_show == app.globalData.tab_is_show) {
                        wx.reLaunch({
                          url: '../industry/industry',
                        })
                      } else {
                        wx.reLaunch({
                          url: '../recruit/index/index',
                        })
                      }
                      // if (app.register_info.register_role == "2") {
                      //   wx.reLaunch({
                      //     url: '../recruit/index/index',
                      //   })
                      // } else {
                      //   wx.reLaunch({
                      //     url: '../recruit/index/index',
                      //   })
                      // }

                    }
                  }
                })
              } else {
                wx.showToast({
                  title: '手机号尚未注册',
                  icon: 'none'
                })
              }
            } else {
              wx.showToast({
                title: e.data.message,
                icon: 'none'
              })
            }

          }
        })
      }
    } else {
      return false;
    }

  },
  goRegister: function() {
    wx.navigateTo({
      url: '../register/register',
    })
  }
})